package com.technology.education.ui.common.feedback

import com.angcyo.dsladapter.dpi
import com.angcyo.dsladapter.dslItem
import com.angcyo.dsladapter.findItemByTag
import com.angcyo.dsladapter.updateOrInsertItem
import com.huantansheng.easyphotos.models.album.entity.Photo
import com.technology.education.R
import com.technology.education.base.BaseRecyclerTopBarFragment
import com.technology.education.dslitem.ItemSingleImage
import com.technology.education.ext.getColor
import com.technology.education.ext.selectPhoto
import com.technology.education.ext.showLoadingExt
import com.technology.education.ext.toast
import me.hgj.jetpackmvvm.ext.view.afterTextChange

class FeedBackFragment : BaseRecyclerTopBarFragment<FeedBackViewModel>() {
    override fun initViews() {
        setTitle("意见反馈")
        initGrid(3)
    }

    override fun lazyLoadData() {
        setUI()
        setBottomButton("提交反馈") {
            closeInput()
            if (mViewModel.content.isEmpty()) {
                toast("请输入您的意见或建议")
                return@setBottomButton
            }
            if (mViewModel.selectedPhotos.isEmpty()) {
                mViewModel.commitData(null)
            } else {
                mViewModel.upload()
            }
        }
    }

    private fun setUI() {
        renderAdapter {
            dslItem(R.layout.item_single_edit) {
                itemTag = "input"
                itemSpanCount = -1
                itemBindOverride = { itemHolder, _, _, _ ->
                    itemTopInsert = 24 * dpi
                    itemDecorationColor = getColor(R.color.white)
                    itemHolder.et(R.id.et_content)?.apply {
                        minLines = 6
                        maxLines = 10
                        hint = "请输入您在使用过程中遇到的问题"
                        textSize = 14f
                        afterTextChange {
                            mViewModel.content = it
                        }
                    }
                }
            }

            dslItem(R.layout.item_upload_image) {
                itemTopInsert = 24 * dpi
                itemSpanCount = 1
                itemDecorationColor = getColor(R.color.white)
                itemTag = "upload"
                itemBindOverride = { itemHolder, _, _, _ ->
                    itemHolder.click(R.id.iv_upload) {
                        selectPhoto(
                            3,
                            selectedList = mViewModel.selectedPhotos as ArrayList<Photo>
                        ) {
                            mViewModel.selectedPhotos = it
                            insertImage(it)
                        }
                    }
                }
            }
        }
    }

    private fun insertImage(photos: ArrayList<Photo>) {
        renderAdapter {
            photos.forEach { photo ->
                updateOrInsertItem<ItemSingleImage>(photo.path, dataItems.size - 1) {
                    it.apply {
                        itemTopInsert = 24 * dpi
                        itemDecorationColor = getColor(R.color.white)
                        imgUrl = photo.path
                        itemSpanCount = 1
                    }
                }
            }
        }
    }

    override fun createObserver() {
        mViewModel.result.observe(this) {
            if (it) {
                toast("感谢您的意见和建议")
                onBack()
            }
        }
    }
}